package com.ask.server.controller;

import com.ask.common.result.Result;
import com.ask.pojo.dto.CategoryDTO;
import com.ask.pojo.dto.CategoryQueryDTO;
import com.ask.pojo.entity.Category;
import com.ask.server.service.ICategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.service.GenericParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Tag(name = "分类接口")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private GenericParameterService parameterBuilder;

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @Operation(summary = "新增分类")
    public Result addCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "根据id删除分类")
    public Result deleteById(@PathVariable Long id) {
        return categoryService.deleteById(id);
    }

    /**
     * 跟新分类
     *
     * @param categoryDTO
     * @return
     */
    @Operation(summary = "跟新分类")
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 启用/禁用分类
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/{id}/status")
    @Operation(summary = "启用/禁用分类")
    public Result updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        categoryService.updateStatus(id, status);
        return Result.success();
    }

    /**
     * 根据类型查询
     *
     * @param type
     * @return
     */
    @GetMapping("/type")
    @Operation(summary = "根据类型查询分类")
    public Result listByType(Integer type) {
        List<Category> categoryList = categoryService.listByType(type);
        return Result.success(categoryList);
    }

    /**
     * 分页查询
     * @param categoryQueryDTO
     * @return
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询")
    public Result pageQuery(@RequestBody CategoryQueryDTO categoryQueryDTO) {
        List<Category> categoryList = categoryService.pageQuery(categoryQueryDTO);
        return Result.success(categoryList);
    }
}